package com.etoak.controller;

import cn.hutool.core.lang.UUID;
import com.etoak.bean.LoginUser;
import com.etoak.bean.ResultVo;
import com.etoak.bean.User;
import com.etoak.service.UserService;
import com.etoak.utils.EmailUtil;
import com.etoak.utils.RedisUtil;
import com.google.code.kaptcha.Producer;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;


@RestController       // @Controller 实例化 + @ResponseBody : 设置响应数据为JSON格式
@RequestMapping("/user")
@CrossOrigin  // 解决跨域问题
public class UserController {
    // 邮箱工具类
    @Resource
    EmailUtil emailUtil;
    // 验证码生成工具
    @Resource
    Producer producer;

    @Resource
    UserService service;

    @Resource
    RedisUtil redisUtil;

    //邮箱登录
    @PostMapping("/emailLogin")
    public ResultVo emailLogin(@RequestBody LoginUser loginUser){
        //验证 验证码
        ResultVo resultVo = validateEmailCode(loginUser);
        if(resultVo.getCode()!=200){
            return resultVo;
        }

        User vo = service.selectByEmail(loginUser.getEmail());
        if(vo==null){
           return  ResultVo.error("账号不存在！");
        }
        vo.setPassword(null);
       return  ResultVo.success().setData(vo);
    }
    //账号密码登录
    @GetMapping("/login")
    public ResultVo login(User user){
        User vo =  service.queryUser(user);
        if(vo==null){
            return  ResultVo.error("账号不存在！");
        }
        if(!vo.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))){
            return  ResultVo.error("密码错误！");
        }
        vo.setPassword(null);
        return  ResultVo.success().setData(vo);
    }

    //注册
    @PostMapping("/add")
    public ResultVo add(@RequestBody User user){
        int i = service.add(user);
        if(i>0) return ResultVo.success();
        return ResultVo.error();
    }
    //查询用户名是否可用
    @GetMapping("/selectByUserName")
    public ResultVo selectByUserName(String username){
         User user = service.selectByUserName(username);
         if(user!=null){
             return ResultVo.error("用户名已存在！");
         }
         return ResultVo.success("用户名可用！");
    }

    //查询邮箱是否可用
    @GetMapping("/selectByEmail")
    public ResultVo selectByEmail(String email){
         User user = service.selectByEmail(email);
         if(user!=null){
             return ResultVo.error("邮箱已被注册！");
         }
         return ResultVo.success("邮箱可用！");
    }
    //获取邮箱验证
    @GetMapping("/emailCaptcha")
    public ResultVo getEmailCode(String email){
        String uuid = UUID.randomUUID().toString();
        //获取随机验证码
        String code = producer.createText();
        System.out.println("============code================" + code);
        //发送邮箱
        // emailUtil.sendMessageToEmail(code,email);
        //根据uuid 存储验证码文本
        redisUtil.hset("email", uuid, code, 180);
        //返回验证码信息
        return ResultVo.success(uuid);
        /*
        return ResultVo.success().setData(MapUtil.builder()
                .put("uuid", uuid)
                .build());
         */
    }

    //校验验证码
    @PostMapping("/validateEmail")
    public ResultVo validateEmail(@RequestBody  LoginUser loginUser ){
        ResultVo resultVo = validateEmailCode(loginUser);
        if(resultVo.getCode()!=200){
            return resultVo;
        }
        return  ResultVo.success("验证成功！");
    }
    //校验验证码公共方法
     public ResultVo validateEmailCode( LoginUser loginUser){
         String code = loginUser.getCode();
         String key = loginUser.getUuid();

         if (StringUtils.isEmpty(code) ) {
             return  ResultVo.error("验证码必填");
         }

         if (StringUtils.isEmpty(key) ) {
             return  ResultVo.error("未获取验证码(uuid)");
         }

         //获取redis中保存的正确验证码
         Object codeObj = redisUtil.hget("email", key);

         if (ObjectUtils.isEmpty(codeObj)){
             return  ResultVo.error("验证码过期了，请重新获取！");
         }
         String saveCode = codeObj.toString();
         if (!code.equals(saveCode)) {
             return  ResultVo.error("验证码错误");
         }

         // 一次性使用
         redisUtil.hdel("email", key);
         return  ResultVo.success();
     }




}
